import os
import sys
os.chdir(sys.path[0])  # 便于在vscode直接运行
import math
import cv2 as cv
import numpy as np

idex = 0
# 读取图片
dir = './pic'  # 图片存放的目录
def readImage(dir, idex):
    """读取图片"""
    filelist = os.listdir(dir)
    imgname = os.path.join(dir, filelist[idex])
    img1 = cv.imread(imgname)  # 这里必须用cv库里面的imread，否则格式不对会报错
    return img1

# 定义一个窗口
def draw_(event, x, y,flag,param):
    global  data, img1
    if event == cv.EVENT_LBUTTONDOWN:
        """左键单击打标签"""
        xy = "%d,%d" % (x, y)
        cv.circle(img1, (x, y),2, (255, 0, 0), thickness=-1)
        data.append([x,y])
 
    if event == cv.EVENT_MBUTTONDOWN:
        """单击鼠标中间，存储数据"""
        file = open('img_label.txt', mode='w+')
        file.writelines(str(data))
        file.close()


cv.namedWindow('image')
cv.setMouseCallback('image', draw_)

 
# 创建一个列表存储标签和RGB值
data = []
label = []
img1 = readImage(dir, idex)
 
while 1:
    cv.imshow('image', img1)
    k = cv.waitKey(1) & 0xFF
    mode = True
    if k == ord('c'): #按c退出
        break
cv.destroyAllWindows()